package me.sauce.dntest.data;

import android.content.Context;

/**
 * Created by sauce on 2017/4/28.
 * Version 1.0.0
 */
public class DatabaseDaoHelper {
    private static final String DB_NAME = "dnTest-db.db";
    private static DatabaseDaoHelper instance;

    private static DaoMaster daoMaster;
    private static DaoSession daoSession;
    private static TimerModelDao timerModelDao;


    /**
     * 取得DaoMaster
     *
     * @param context applicationContext
     * @return DaoMaster
     */
    private static DaoMaster getDaoMaster(Context context) {
        if (daoMaster == null) {
            daoMaster = new DaoMaster(new DaoMaster.DevOpenHelper(context, DB_NAME).getWritableDb());
        }
        return daoMaster;
    }

    /**
     * 取得DaoSession
     *
     * @param context applicationContext
     * @return DaoSession
     */
    private static DaoSession getDaoSession(Context context) {
        if (daoSession == null) {
            if (daoMaster == null) {
                daoMaster = getDaoMaster(context);
            }
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }


    public static DatabaseDaoHelper getInstance() {
        return instance;
    }

    public static void init(Context context) {
        instance = new DatabaseDaoHelper();
        // 数据库对象
        DaoSession daoSession = getDaoSession(context);
        timerModelDao = daoSession.getTimerModelDao();
    }

    public  TimerModelDao getTimerModelDao() {
        return timerModelDao;
    }
}
